package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hbpa.dao.StudentJfDao;
import com.hbpa.form.StudentJfItem;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;

public class StudentJfDaoImpl implements StudentJfDao {

	public boolean addStudentJf(String xh, String sjd, String jfzl, String jldw) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into st_jf(xh,sjd,jfzl,jldw) values(?,?,?,?)");
			ps.setString(1, xh);
			ps.setString(2, sjd); 
			ps.setString(3, jfzl);
			ps.setString(4, jldw); 
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean checkStudentJf(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from st_jf where xh = ?");
			 
			ps.setString(1, xh);
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(!count.equals("0")){
					return true;
				}else{
					return false;
				}
			}
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

	public boolean deleteAllStudentJf(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from st_jf where xh=?");
			ps.setString(1, xh); 
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean deleteStudentJf(String id, String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from st_jf where id=? and xh=?");
			ps.setString(1, id);
			ps.setString(2, xh);
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public List<StudentJfItem> getStudentJfByXh(String xh) {
		if(checkStudentJf(xh)==false)
		{
			return null;
		}
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		List<StudentJfItem> studentJfItem = new ArrayList<StudentJfItem>();
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from st_jf where xh = ? order by id asc");
			 
			ps.setString(1, xh);
			rs = ps.executeQuery();
			while(rs.next())
			{   
				StudentJfItem item = new StudentJfItem();
				item.setId(rs.getString("id"));
				item.setXh(rs.getString("xh"));
				item.setJfzl(rs.getString("jfzl"));
				item.setJldw(rs.getString("jldw"));
				item.setSjd(rs.getString("sjd"));
				studentJfItem.add(item);
			}
			 return studentJfItem;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
		return null;
	}

}
